// pages/classroom/classroom.js
var r = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classList:[
      // {id:'4424',title:'党建第一节',status:0},
      // {id:'4735745',title:'党建第二节',status:1}
    ],
    active:0,

    list:[
      {id:1,isExam:0},
      {id:2,isExam:1},
      {id:3,isExam:1},
    ],
    page: 1,
    pages: 0,

    show:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 修改顶部导航颜色
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#CB1C1D'
    })
  },

  // 更多
  more(){
    wx.navigateTo({
      url: '/pages/all/pages/more/more'
    })
  },

  // 查看课程
  see(e){
    let status = e.currentTarget.dataset.status
    let id = e.currentTarget.dataset.id
    if(status == 1){
      wx.navigateTo({
        url: '/pages/all/pages/moreDetailVideo/moreDetailVideo?id='+id
      })
    }else if(status == 2){
      wx.navigateTo({
        url: '/pages/all/pages/moreDetailImg/moreDetailImg?id='+id
      })
    }
  },


  //加载数据列表
  datalist(pageNo) {
    this.loading = true
    wx.showLoading()
    let isExam = ''
    if(this.data.active == 1){
      isExam = 1
    }else if(this.data.active == 2){
      isExam = 0
    }
    var param = {
      isExam:isExam,
      page: pageNo,
      limit: 10
    };
    //获取数据列表
    r.toGet('/exam/paperList', param, (res) => {
      this.loading = false
      wx.hideLoading()
      if (!res.retData || !res.retData.records || res.retData.records.length == 0) {
        wx.showToast({
          title: '无数据',
          icon:'none'
        })
        this.setData({
          show:true
        })
        return
      } else {
        this.setData({
          show:false,
          page: pageNo, //当前的页号
          pages: res.retData.pages, //总页数
          list: this.data.list.concat(res.retData.records)
        })
      }
    })
  },


  // 底部切换
  tabs(e){
    let num = e.currentTarget.dataset.num
    this.setData({
      active:num,
      page: 1,
      pages: 0,
      list: []
    })
    this.datalist(1);
  },

  // 考试或详情
  getInto(e){
    let status = e.currentTarget.dataset.status
    let id = e.currentTarget.dataset.id
    if(status == 0){
      // 考试入口
      wx.navigateTo({
        url: '/pages/all/pages/getExamination/getExamination?id='+id,
      })
    }else{
      // 查看成绩
      wx.navigateTo({
        url: '/pages/all/pages/examinationBranch/examinationBranch?id='+id,
      })
    }
  },

  // 底部页面跳转
  skip(e) {
    let num = e.currentTarget.dataset.num
    if (num == 1) {
      wx.reLaunch({
        url: '/pages/index/index',
      })
    } else if (num == 3) {
      wx.reLaunch({
        url: '/pages/understand/understand',
      })
    } else if (num == 4) {
      wx.reLaunch({
        url: '/pages/my/my',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /** 
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 在线学习
    r.toGet('/course/courseList', {page:1,limit:2}, (res) => {
      // console.log(res.retData.records)
      res.retData.records.forEach(item=>{
        item.createTime = item.createTime.split(' ')[0]
        item.createTime = item.createTime.split('-')[1] + '.' + item.createTime.split('-')[2]
      })
      this.setData({
        classList:res.retData.records
      })
    })


    this.setData({
      page: 1,
      pages: 0,
      list: []
    })
    this.datalist(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.loading && this.data.page < this.data.pages) {
      this.datalist(this.data.page + 1)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})